import uuid
import profile
import helpers.FileHelper as FileHelper
import helpers.MiscHelper as MiscHelper
import script.LuaMerger as LuaMerger



class AmazingFeature:

    def __init__(self, root_path):
        self.path = root_path
        self.conf = FileHelper.r_json(root_path, "content.json")

    def rehash(self):
        MiscHelper.json_set(self.conf, uuid.uuid1().hex, "tag")
        FileHelper.w_json(self.conf, self.path, "content.json")

    def convert(self, binary):
        if not FileHelper.exists(profile.amazing_editor_path):
            raise "AmazingEditor is not exists"
        src = self.path
        dst = FileHelper.solve_path(profile.effect_temporary, ".convert")
        exe = FileHelper.solve_path(profile.amazing_editor_path, "Contents", "MacOS", "AmazingEditor")
        FileHelper.prepare_dir(dst, "*", clear=True)
        FileHelper.sys_exec('''"{}" --converter "{}" {} -o "{}"''', exe, src, "-b" if binary else "-t", dst)
        FileHelper.cp_(dst, src)

    def merge(self):
        root_path = FileHelper.solve_path(self.path, "lua", "src")
        LuaMerger.merge(root_path, "MainEntry", FileHelper.solve_path(self.path, "lua", "GraphSystem.lua"))
        LuaMerger.merge(root_path, "CCEntry", FileHelper.solve_path(self.path, "lua", "SeekModeScript.lua"))
        LuaMerger.merge(root_path, "XTEntry", FileHelper.solve_path(self.path, "lua", "SeekModeScript.lua"))
        LuaMerger.merge(root_path, "TREntry", FileHelper.solve_path(self.path, "lua", "TransitionScript.lua"))
        LuaMerger.merge(root_path, "VAEntry", FileHelper.solve_path(self.path, "lua", "SeekModeScript.lua"))

    def remove_slider_config(self):
        FileHelper.rm(self.path, "ImageBusinessSlider.json")